<?php
namespace app\massage\controller;
use app\AdminRest;
use app\massage\model\Coach;
use app\massage\model\CommissionType;
use app\massage\model\Identity;
use app\massage\model\IdentityList;
use app\massage\model\Order;
use app\massage\model\User;
use app\massage\model\Wallet;
use app\massage\model\WorkLog;
use think\App;



class AdminIdentity extends AdminRest
{
    protected $model;
    protected $wallet_model;
    protected $commission_type_model;
    protected $identity_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model = new Identity();
        $this->wallet_model = new Wallet();
        $this->commission_type_model = new CommissionType();
        $this->identity_model = new Identity();
    }

    /**
     * @return \think\Response
     * 身份列表
     */
    public function list()
    {
        $input = $this->_param;
        $dis = [];
        $data = $this->model->list($dis,$input['limit']);
        return $this->success($data);
    }

    //身份详情
    public function info()
    {
        $input = $this->_param;
        $dis = [
            'id' => $input['id']
        ];
        $data = $this->model->dataInfo($dis);
        if($data['rule']){
            $data['rule'] = json_decode($data['rule'],true);
        }else{
            $data['rule'] = [];
        }
        return $this->success($data);
    }
    /**
     * @return \think\Response
     * 添加身份
     */
    public function identityAdd()
    {
        $input = $this->_param;
        $res = $this->model->dataAdd($input);
        return $this->success($res);
    }

    /**
     * @return \think\Response
     * 编辑身份
     */
    public function identityUpdate()
    {
        $input = $this->_param;
        $id = $input['id'];
        unset($input['id']);
        $res = $this->model->dataUpdate(['id' => $id],$input);
        return $this->success($res);
    }

    /**
     * @功能说明:身份审核(2通过,3取消,4拒绝)
     */
    public function identityAuditUpdate(){

        $input = $this->_param;
        $identity_list_model = new IdentityList();

        $identity = $identity_list_model->dataInfo(['id' => $input['id']]);
        if(!empty($input['status'])&&in_array($input['status'],[2,4]) && $identity['status'] == 0){
            $input['sh_time'] = time();
        }
        $params = $input;
        unset($params['id']);
        $res = $identity_list_model->dataUpdate(['id' => $input['id']],$params);
        if($input['status'] == 2){
            //审核通过
            $res = $this->handlePass($input);
        }
        return $this->success($res);
    }

    //处理审核通过
    public function handlePass($input)
    {
        $identity_model = new Identity();
        $identity_list_model = new IdentityList();
        $coach_model = new Coach();
        $user_model = new User();

        $identity = $identity_list_model->dataInfo(['id' => $input['id']]);
        $id_info = $identity_model->dataInfo(['id' => $identity['identity_id']]);
        switch ($id_info['type']){
            case 1: //技师
                $data = [
                    'sh_text' => $identity['sh_text'],
                    'sh_time' => $identity['sh_time'],
                    'status' => 2,
                ];
                $coach_model->dataUpdate(['user_id' => $input['id']],$data);
                $data = [
                    'identity_id' => $identity['identity_id'],
                    'coach_level_id' => 1,
                ];
                //云店
                if($identity['identity_id'] > 1){
                    $data['coach_level_id'] = 3;
                }
                $user_model->dataUpdate(['id' => $identity['user_id']],$data);
                break;
            case 3: //门店
                $data = [
                    'identity_id' => $identity['identity_id'],
                ];
                $user_model->dataUpdate(['id' => $identity['user_id']],$data);
                break;
            case 2: //代言人
            default:
                break;
        }
    }

    //身份申请列表
    public function applyList()
    {
        $identity_list_model = new IdentityList();
        $input = $this->_param;
        $dis = [];
        $data = $identity_list_model->list($dis,$input['limit']);
        return $this->success($data);
    }

    //身份申请列表
    public function applyInfo()
    {
        $identity_list_model = new IdentityList();
        $identity_model = new Identity();
        $user_model = new User();
        $input = $this->_param;
        $dis = ['id' => $input['id']];
        $data = $identity_list_model->dataInfo($dis);
        if(empty($data)){
            return $this->success($data);
        }
        $data['identity_name'] = $identity_model->where(['id' => $data['identity_id']])->value('name');
        $data['user'] = $user_model->dataInfo(['id' => $data['user_id']]);
        $data['content'] = json_decode($data['content'],true);
        return $this->success($data);
    }

    //获取所有佣金类型列表
    public function commissionTypeList()
    {
        $input = $this->_param;
        $dis = [];
        $data = $this->commission_type_model->dataList($dis,'id,name');
        return $this->success($data);
    }

    //设置佣金比例
    public function setCommissionType()
    {
        $input = $this->_param;
        $this->identity_model->dataUpdate(['id' => $input['id']],['rule' => json_encode($input['rule'])]);
        return $this->success('操作成功');
    }
}
